//
//  Card.m
//  CardSwitchDemo
//
//  Created by Apple on 2016/11/9.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "XLCard.h"
#import "XLCardItem.h"
#import "ADView.h"
#import "AddAndCutView.h"

@interface XLCard () {
    UIImageView *_imageView;//背景图片
    UIScrollView *_bgView;//背景图片
    UILabel *_textLabel;
    ADView * _adView;
    UILabel *_priceLabel;
    UILabel *_stockLabel;//库存label
    AddAndCutView *_countView;
    UIView *_lineView;
    UIView *_detailView;
    UILabel *_d_textLabel;
    UILabel *_d_detailLabel;
    NSMutableArray * _AdArray;
}
@end

@implementation XLCard

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initSubViews];
        [self buildUI];
    }
    return self;
}
-(void)initSubViews
{
    _imageView = [[UIImageView alloc] init];
    [self addSubview:_imageView];
    _bgView = [[UIScrollView alloc]init];
    [_imageView addSubview:_bgView];
    _adView = [[ADView alloc]init];
    [_bgView addSubview:_adView];
    _textLabel = [[UILabel alloc] init];
    [_bgView addSubview:_textLabel];
    _priceLabel = [[UILabel alloc] init];
    [_bgView addSubview:_priceLabel];
    _stockLabel = [[UILabel alloc] init];
    [_bgView addSubview:_stockLabel];
    
    _countView = [[AddAndCutView alloc] init];
    [_bgView addSubview:_countView];
    _lineView = [[UILabel alloc] init];
    [_bgView addSubview:_lineView];
    _detailView = [[UIView alloc] init];
    [_bgView addSubview:_detailView];
    _d_textLabel = [[UILabel alloc] init];
    [_detailView addSubview:_d_textLabel];
    _d_detailLabel = [[UILabel alloc] init];
    [_detailView addSubview:_d_detailLabel];

}
- (void)buildUI {
    self.layer.cornerRadius = 10.0f;
//    [self setBianKuangAndColor:ShenLine_color];
    self.backgroundColor = [UIColor whiteColor];
    CGFloat viewW = self.bounds.size.width;
    CGFloat ViewH = self.bounds.size.height;
    CGFloat imageW = viewW;
    CGFloat imageH = imageW/(1200/814.);
    CGFloat labelH = Nomal_Label_H;
    CGFloat jianGe = 5;
    CGFloat jianGeW = 5;
    _imageView.frame = CGRectMake(0, 0, viewW, ViewH);
//    _imageView.contentMode = UIViewContentModeScaleAspectFill;
//    _imageView.backgroundColor = [UIColor blueColor];
    UIImage * image = [UIImage imageNamed:@"wuliaoBg"];
    [image stretchImageWithInsetsTop:jianGe left:jianGe bottom:jianGe right:jianGe];
    _imageView.image = image;
    _imageView.userInteractionEnabled = YES;
    _bgView.frame = CGRectMake(jianGeW, jianGe, viewW-jianGeW*2, ViewH-jianGe*2);
    _bgView.showsVerticalScrollIndicator = NO;
    _bgView.showsHorizontalScrollIndicator = NO;
//    _bgView.backgroundColor = [UIColor greenColor];
    viewW = viewW-jianGeW*2;
    [self loadADView];
    _textLabel.frame = CGRectMake(0, _adView.frame.size.height+_adView.frame.origin.y+jianGe, viewW, labelH);
    _textLabel.textColor = RGBColorAlpha(45, 45, 45, 1);
    _textLabel.font = [UIFont systemFontOfSize:16];
    _textLabel.textAlignment = NSTextAlignmentCenter;
    _textLabel.adjustsFontSizeToFitWidth = true;
    CGFloat labelW = 100;
    _priceLabel.frame = CGRectMake((viewW-labelW)/2.0, _textLabel.frame.origin.y+_textLabel.frame.size.height+20, labelW, labelH);
    _priceLabel.textColor = RGBColorAlpha(255, 66, 89, 1);
    _priceLabel.font = [UIFont systemFontOfSize:18];
    _priceLabel.textAlignment = NSTextAlignmentCenter;
    
    _stockLabel.frame = CGRectMake(viewW-labelW, _priceLabel.frame.origin.y, labelW, labelH);
    _stockLabel.textColor = QianZi_color;
    _stockLabel.font = [UIFont systemFontOfSize:14];
    _stockLabel.textAlignment = NSTextAlignmentCenter;
    CGFloat c_ViewW = 180;
    _countView.frame = CGRectMake((viewW-c_ViewW)/2.0, _priceLabel.frame.origin.y+_priceLabel.frame.size.height+10, c_ViewW, 40);
    [_countView loadAddAndCutView];
    _lineView.backgroundColor = TAB_BAR_COLOR;
    CGFloat jiangeW = 20;
    _lineView.frame = CGRectMake(jiangeW, _countView.frame.origin.y+_countView.frame.size.height+20, viewW-jiangeW*2, 1);
    _detailView.frame = CGRectMake(0, _lineView.frame.origin.y+_lineView.frame.size.height+20, viewW, ViewH-(_lineView.frame.origin.y+_lineView.frame.size.height+20));
    _d_textLabel.text  =@"详情描述:";
    _d_textLabel.font = Cell_Big_Font;
    _d_textLabel.textColor = ZhongZi_Color;
    _d_textLabel.textAlignment = NSTextAlignmentCenter;
    _d_textLabel.frame = CGRectMake(0, 0, viewW, Nomal_Label_H);
    _d_detailLabel.text = @"说得很干净大方深度和高度和改善到肌肤的孤独韩国三大功夫的如何使代表大会方便很多自己看得不够，速度会更好的风景，呃合肥市规划局是卡卡。";
    _d_detailLabel.font = Cell_Medium_Font;
    _d_detailLabel.textColor = QianZi_color;
    _d_detailLabel.numberOfLines = 0;
    _d_detailLabel.frame = CGRectMake(jiangeW, Nomal_Label_H+10, viewW-jiangeW*2, Nomal_Label_H);
//    _d_detailLabel.textAlignment = NSTextAlignmentCenter;
    [_d_detailLabel sizeToFit];
    _d_detailLabel.frame = CGRectMake(jiangeW, Nomal_Label_H+10, viewW-jiangeW*2, _d_detailLabel.frame.size.height);
    _detailView.frame =  CGRectMake(0, _lineView.frame.origin.y+_lineView.frame.size.height+20, viewW, _d_detailLabel.frame.size.height+_d_detailLabel.frame.origin.y);
    _bgView.contentSize = CGSizeMake(viewW, _detailView.frame.size.height+_detailView.frame.origin.y+20);
    
    
}

-(void)loadADView//设置广告位
{
    CGFloat jianGe = 5;
    CGFloat viewW = self.bounds.size.width-jianGe*2;
    CGFloat imageW = viewW;
    CGFloat adView_H = imageW/(1200/814.);
    NSMutableArray * arr = [[NSMutableArray alloc]init];
    NSArray * imageArr = [NSArray arrayWithObjects:@"wuliaoPhoto",@"wuliaoPhoto",@"wuliaoPhoto",@"wuliaoPhoto", nil];
    for (int i=0; i<imageArr.count; i++) {
        ADModel* model = [[ADModel alloc]init];
        model.cover = [imageArr objectAtIndex:i];
        //adView内部的默认图片改了，记得改回来。
        [arr addObject:model];
    }
    _AdArray = arr;
    _adView.frame = CGRectMake(0, 0,imageW ,adView_H);
    _adView.layer.masksToBounds = YES;
    [_adView startAnimation:NO];
    [_adView loadADViewWithNumber:[_AdArray count] WithImageFrame:CGSizeMake(imageW, adView_H) WithHorizontal:YES WithAnimationTime:2.0];
   
    NSMutableArray *tmpImageArray = [[NSMutableArray alloc] init];
    for (int i=0; i<[_AdArray count]; i++) {
        ADModel* model = [_AdArray objectAtIndex:i];
        NSString *productADString  = model.cover;
        [tmpImageArray addObject:productADString];
    }
    //    _adView.imageColor = [UIColor redColor];
    if ([_AdArray count]>0) {
        [_adView initNOFitImageViewWithImagesURL:tmpImageArray andPlaceholderImageName:DEFAULT_HOMEAD_PHOTO_RECTANGLE];
    }
    [_adView addPageControl];
    
    
}

-(void)setItem:(XLCardItem *)item {
//    _imageView.image = [UIImage imageNamed:item.imageName];
    _stockLabel.text = [NSString stringWithFormat:@"(库存%@件)",@"100"];
    _priceLabel.text = [NSString stringWithFormat:@"¥%@",@"1000"];
    _textLabel.text = item.title;
}

@end
